var prefix = jsctx + "/tenantAppInfo";

var tenantAppInfo = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;
    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageTenantAppInfo",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 20,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    paginationShowPageGo: true,
                    height: h,
                    // rightFixedColumns: true,
                    // showColumns: true,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            //elecStationId: $("#elecStationId").val().trim(),
                            //  operateStatus: $("#operateStatus").val().trim(),
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {
                            field: 'upgradeType', title: '更新类型', width: '100',
                            align: 'center',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '版本升级'
                                } else if (value == '1') {
                                    return '版本回滚'
                                }
                            }
                        },
                        {field: 'appVersion', title: '当前版本号', align: 'left', width: '100'},
                        {field: 'publishTime', title: '当前版本发布时间', align: 'left', width: '100'},
                        {field: 'versionName', title: '版本名称', align: 'left', width: '100'},
                        {field: 'versionDesc', title: '版本描述', align: 'left', width: '100'},
                        {field: 'appLocation', title: '安装文件下载路径', align: 'left', width: '100'},
                        {field: 'osMinVersion', title: '手机系统需求最低版本', align: 'left', width: '150'},
                        {
                            field: 'isMustUpdate', title: '是否强制更新',
                            align: 'center',
                            width: '100',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '否'
                                } else if (value == '1') {
                                    return '是'
                                }
                            }
                        },
                        {
                            field: 'status', title: '审核状态',
                            align: 'center',
                            width: '100',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '未审核'
                                } else if (value == '1') {
                                    return '审核通过'
                                } else if (value == '2') {
                                    return '审核不通过'
                                }
                            }
                        },
                        {
                            title: '操作',
                            field: 'roleId',
                            align: 'center',
                            width: '100',
                            formatter: function (value, row, index) {

                                var a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="通过" onclick="tenantAppInfo.pass(\''
                                    + row.appUpgradeId
                                    + '\')">通过</a> ';

                                var b = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="驳回" onclick="tenantAppInfo.overrule(\''
                                    + row.appUpgradeId
                                    + '\')">驳回</a> ';

                                if (row.status == 0) {
                                    return a + b;
                                }

                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function addTenantAppInfo() {
        location.href = prefix + "/addTenantAppInfo";
    }

    function pass(appUpgradeId) {
        layer.confirm('确定要通过？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/review",
                type: "post",
                data: {
                    'appUpgradeId': appUpgradeId,
                    'status': '0'

                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("变更成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function overrule(appUpgradeId) {
        layer.confirm('确定要驳回？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/review",
                type: "post",
                data: {
                    'appUpgradeId': appUpgradeId,
                    'status': '1'
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("变更成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function goBack() {
        window.history.back();
    }


    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        addTenantAppInfo: addTenantAppInfo,
        pass: pass,
        overrule: overrule
    };
})();

var addTenantAppInfo = (function () {
    function init() {
        validateRule();
    };

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    var isajaxsend = true;

    function save() {
        if (isajaxsend) {
            var formData = new FormData($("#frmTenantAppInfo")[0]);
            $("#roleIds").val(getCheckedRoles());
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/saveOrUpdateTenantAppInfo",
                data: formData,
                processData: false,
                contentType: false,
                async: false,
                beforeSend: function () {
                    isajaxsend = false
                },
                error: function (request) {
                    parent.layer.alert("Connection error");
                    isajaxsend = true;
                },
                success: function (data) {
                    if (data.status == 200) {
                        isajaxsend = false
                        parent.layer.msg("操作成功");
                        window.history.back();
                    } else {
                        parent.layer.alert(data.message)
                        isajaxsend = true;
                    }

                }
            });
        }
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmTenantAppInfo").validate({
            rules: {
                pileNo: {
                    required: true
                },
                pileName: {
                    required: true
                }

            },
            messages: {
                pileNo: {
                    required: icon + "请输入桩群编号"
                },
                pileName: {
                    required: icon + "请输入桩群名称"
                }
            },
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init
    };
})();
